﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DBUtility;

namespace SQLServerDAL
{
    /// <summary>
    /// 商品类别管理
    /// </summary>
    public class ProductCategoryBase
    {
        private SqlParameter[] Para;
        private SqlConnection Conn;

        public ProductCategoryBase()
        {
            Conn = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction);
        }

        /// <summary>
        /// 新建目录
        /// </summary>
        public string CreateNew(Model.ProductCategoryBase category)
        {
            string result = "";
            SqlParameter[] parameters = new SqlParameter[] {
				new SqlParameter("@CategoryNumber", SqlDbType.VarChar, 1024),
				new SqlParameter("@CategoryCode", SqlDbType.VarChar, 256),
				new SqlParameter("@CategoryName", SqlDbType.VarChar, 500),
				new SqlParameter("@ParentId", SqlDbType.UniqueIdentifier),
				new SqlParameter("@Layer", SqlDbType.Int,4),
				new SqlParameter("@LineNum", SqlDbType.Int,4),
				new SqlParameter("@Description", SqlDbType.NVarChar),
				new SqlParameter("@CreatedOn", SqlDbType.DateTime),
				new SqlParameter("@CreatedBy", SqlDbType.UniqueIdentifier),
				new SqlParameter("@Picture", SqlDbType.VarChar),
				new SqlParameter("@ItemsCount", SqlDbType.Int),
				new SqlParameter("@ApprovalsList", SqlDbType.VarChar),
				new SqlParameter("@CategoryId", SqlDbType.UniqueIdentifier,16,"Output"),
                new SqlParameter("@ReturnValue", SqlDbType.Int)
            };
            parameters[0].Value = category.CategoryNumber;
            parameters[1].Value = category.Code;
            parameters[2].Value = category.CategoryName;
            parameters[3].Value =  new Guid(category.ParentId);
            parameters[4].Value = category.Layer;
            parameters[5].Value = category.LineNum;
            parameters[6].Value = category.Description;
            parameters[7].Value = category.ModifiedOn;
            parameters[8].Value = new Guid(category.ModifiedBy);
            parameters[9].Value = category.Picture;
            parameters[10].Value = category.ItemsCount;
            parameters[11].Value = category.ApprovalsList;
            parameters[12].Direction = ParameterDirection.Output;
            parameters[13].Direction = ParameterDirection.ReturnValue;
 
            try
            {
                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.StoredProcedure, "ProductCategory_Create", parameters);
                int status = ((parameters[13].Value != null) ? ((int)parameters[13].Value) : -1);
                if (status == 0)
                {
                    result = "succeeded" + "|" + parameters[12].Value.ToString().Trim();
                }
                else 
                {
                    result = status.ToString();
                }
            }
            catch (Exception err)
            {
                result = err.Message; ///procedure error
            }
            return result;
        }

        /// <summary>
        /// 修改目录
        /// </summary>
        /// <param name="subject">对象</param>
        /// <param name="intType">操作属性,0未修改父目录,1为修改父目录</param>
        public string Modify(Model.ProductCategoryBase category, bool IsChangeParent)
        {
            string result = "";
            SqlParameter[] parameters = new SqlParameter[] {
					new SqlParameter("@CategoryNumber", SqlDbType.VarChar, 1024),
					new SqlParameter("@CategoryCode", SqlDbType.VarChar, 256),
					new SqlParameter("@CategoryName", SqlDbType.VarChar, 500),
					new SqlParameter("@ParentId", SqlDbType.UniqueIdentifier),
					new SqlParameter("@Layer", SqlDbType.Int,4),
					new SqlParameter("@LineNum", SqlDbType.Int,4),
					new SqlParameter("@Description", SqlDbType.NVarChar, 4000),
				    new SqlParameter("@ModifiedOn", SqlDbType.DateTime),
				    new SqlParameter("@ModifiedBy", SqlDbType.UniqueIdentifier),
					new SqlParameter("@CategoryId", SqlDbType.UniqueIdentifier),
					new SqlParameter("@IsChangeParent", SqlDbType.Bit),
				    new SqlParameter("@Picture", SqlDbType.VarChar), 
				    new SqlParameter("@ApprovalsList", SqlDbType.VarChar),
                    new SqlParameter("@ReturnValue", SqlDbType.Int)
                };
            parameters[0].Value = category.CategoryNumber;
            parameters[1].Value = category.Code;
            parameters[2].Value = category.CategoryName;
            parameters[3].Value =  new Guid(category.ParentId);
            parameters[4].Value = category.Layer;
            parameters[5].Value = category.LineNum;
            parameters[6].Value = category.Description;
            parameters[7].Value = category.ModifiedOn;
            parameters[8].Value = new Guid(category.ModifiedBy);
            parameters[9].Value = new Guid(category.CategoryId);
            parameters[10].Value = IsChangeParent;
            parameters[11].Value = category.Picture;
            parameters[12].Value = category.ApprovalsList;
            parameters[13].Direction = ParameterDirection.ReturnValue;
 
            try
            {
                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.StoredProcedure, "ProductCategory_Update", parameters);
                int status = ((parameters[13].Value != null) ? ((int)parameters[13].Value) : -1);
                if (status == 0)
                {
                    result = "succeeded";
                }
                else
                {
                    result = status.ToString();
                }
            }
            catch(Exception err)
            {
                result = err.Message; ///procedure error
            }

            return result;
        }

        #region 获取目录信息实体
        /// <summary>
        /// 获取目录信息实体
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        public Model.ProductCategoryBase GetObjById(string categoryId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ProductCategoryBase WHERE CategoryId = @CategoryId AND DeletionStateCode=0");

            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@CategoryId", SqlDbType.UniqueIdentifier) };
            parameters[0].Value = new Guid(categoryId);

            Model.ProductCategoryBase _categoryObj = null;
            try
            {
                SqlDataReader rd = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, strSql.ToString(), parameters);
                if (rd.Read())
                {
                    _categoryObj = new Model.ProductCategoryBase();
                    _categoryObj.CategoryNumber = rd["CategoryId"].ToString();
                    _categoryObj.Code = rd["CategoryCode"].ToString();
                    _categoryObj.CategoryName = rd["CategoryName"].ToString();
                    _categoryObj.ParentId = rd["ParentId"].ToString();
                    _categoryObj.Layer = int.Parse(rd["Layer"].ToString());
                    _categoryObj.LineNum = int.Parse(rd["LineNum"].ToString());
                    _categoryObj.Description = rd["Description"].ToString();
                    _categoryObj.ModifiedOn = DateTime.Parse(rd["ModifiedOn"].ToString());
                    _categoryObj.ModifiedBy = rd["ModifiedBy"].ToString();
                    _categoryObj.CreatedOn = DateTime.Parse(rd["CreatedOn"].ToString());
                    _categoryObj.CreatedBy = rd["CreatedBy"].ToString();
                    _categoryObj.Picture = rd["Picture"].ToString();
                    _categoryObj.ApprovalsList = (!string.IsNullOrEmpty(rd["ApprovalsList"].ToString())) ? rd["ApprovalsList"].ToString() : "";
                    _categoryObj.ItemsCount = int.Parse((!string.IsNullOrEmpty(rd["ItemsCount"].ToString())) ? rd["ItemsCount"].ToString() : "0");
                }
            }
            catch
            {
                return null;
            }
            return _categoryObj;
        }
        #endregion

        /// <summary>
        /// 删除目录
        /// </summary> 
        public string Delete(string categoryId, bool isLogical)
        {
            string result = "";
            SqlParameter[] parameters = new SqlParameter[] {
					new SqlParameter("@CategoryId", SqlDbType.UniqueIdentifier),
					new SqlParameter("@IsLogical", SqlDbType.Bit),
                    new SqlParameter("@ReturnValue", SqlDbType.Int)
                };
            parameters[0].Value = new Guid(categoryId);
            parameters[1].Value = isLogical;
            parameters[2].Direction = ParameterDirection.ReturnValue;
 
            try
            {
                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.StoredProcedure, "ProductCategory_Delete", parameters);
                int status = ((parameters[2].Value != null) ? ((int)parameters[2].Value) : -1);
                switch (status)
                {
                    case 0:
                        result = "succeeded";
                        break;
                    default:
                        result = status.ToString();
                        break; 
                }
            }
            catch(Exception err)
            {
                result = err.Message;
            }

            return result;
        }
    }
}
